home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / xlib03.zip / XCBITMAP.ASM < prev    next >
Assembly Source File  |  1993-04-05  |  9KB  |  280 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XCBITMAP
  3. ; This module was written by Matthew MacKenzie
  4. ; matm@eng.umd.edu
  5. ;
  6. ; Compiled bitmap  functions all MODE X 256 Color resolutions
  7. ;
  8. ; Compile with Tasm.
  9. ; C callable.
  10. ;
  11. ; ****** XLIB - Mode X graphics library                ****************
  12. ; ******                                               ****************
  13. ; ****** Written By Themie Gouthas                     ****************
  14. ; ****** Aeronautical Research Laboratory              ****************
  15. ; ****** Defence Science and Technology Organisation   ****************
  16. ; ****** Australia                                     ****************
  17. ;
  18. ; egg@dstos3.dsto.gov.au
  19. ; teg@bart.dsto.gov.au
  20. ;-----------------------------------------------------------------------
  21.  
  22. include xlib.inc
  23. include xcbitmap.inc
  24.  
  25.  
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  27. ; _x_compile_bitmap
  28. ;
  29. ; Compile a linear bitmap to generate machine code to plot it
  30. ; at any required screen coordinates FAST. Faster than blits
  31. ; using planar bitmaps as in module XPBIITMAP
  32. ;
  33. ; C near-callable as:
  34. ; x_compile_bitmap  (int logical_screen_width,
  35. ;                char far * bitmap, char far * output);
  36. ;
  37. ; The logical width is in bytes rather than pixels.
  38. ;
  39. ; All four main registers are totaled.
  40. ;
  41. ;  The source linear bitmaps have the following structure:
  42. ;
  43. ;  BYTE 0                 The bitmap width in pixels  range 1..255
  44. ;  BYTE 1                 The bitmap height in rows   range 1..255
  45. ;  BYTE 2..n              The width*height bytes of the bitmap in
  46. ;                         cloumn row order
  47.  
  48.  
  49.  
  50. ; accessory macros to save typing (what else?)
  51. Emitb macro arg
  52.     mov byte ptr es:[di],&arg&
  53.     inc di
  54.     endm
  55.  
  56. Emitw macro arg
  57.     mov word ptr es:[di],&arg&
  58.     add di,2
  59.     endm
  60.  
  61. ; opcodes emitted by _x_compile_sprite
  62. SHR_CX          equ 0e9d1h              ; shr cx
  63. ROL_AL          equ 0c0d0h              ; rol al
  64. SHORT_STORE_8   equ 044c6h              ; mov [si]+disp8,  imm8
  65. STORE_8         equ 084c6h              ; mov [si]+disp16, imm8
  66. SHORT_STORE_16  equ 044c7h              ; mov [si]+disp8,  imm16
  67. STORE_16        equ 084c7h              ; mov [si]+disp16, imm16
  68. ADC_SI_IMMED    equ 0d683h              ; adc si,imm8
  69. OUT_AL          equ 0eeh                ; out dx,al
  70. RETURN          equ 0cbh                ; ret
  71.  
  72.  
  73. .data
  74.  
  75. align 2
  76. ColumnMask      db      011h,022h,044h,088h
  77. ColumnAdds      db      00h, 04h, 02h, 01h
  78.  
  79.  
  80. .code
  81.  
  82.     align   2
  83. _x_compile_bitmap proc
  84. ARG   logical_width:word,bitmap:dword,output:dword
  85. LOCAL bwidth,scanx,scany,outputx,outputy,column,set_column,input_size:word=LocalStk
  86.     push bp
  87.     mov  bp, sp                        ; caller's stack frame
  88.     sub  sp,LocalStk                   ; local space
  89.     push si
  90.     push di
  91.     push ds
  92.  
  93.     mov word ptr [scanx],0
  94.     mov word ptr [scany],0
  95.     mov word ptr [outputx],0
  96.     mov word ptr [outputy],0
  97.     mov word ptr [column],0
  98.     mov word ptr [set_column],0
  99.  
  100.     lds si,[bitmap]                   ; 32-bit pointer to source bitmap
  101.  
  102.     les di,[output]                   ; 32-bit pointer to destination stream
  103.  
  104.     lodsb                             ; load width byte
  105.     xor ah,ah                         ; convert to word
  106.     mov [bwidth],ax                   ; save for future reference
  107.     mov bl,al                         ; copy width byte to bl
  108.     lodsb                             ; load height byte. Is word since ah=0
  109.     mul bl                            ; mult height word by width byte
  110.     mov [input_size],ax               ;  giving pixel total and save
  111.  
  112. MainLoop:
  113.         mov bx,[scanx]                ; position in original bitmap
  114.         add bx,[scany]
  115.  
  116.     mov al,[si+bx]                ; get pixel
  117.     or  al,al                     ; skip empty pixels
  118.     jnz NoAdvance
  119.     jmp Advance
  120. NoAdvance:
  121.  
  122.             mov dx,[set_column]
  123.             cmp dx,[column]
  124.             je SameColumn
  125. ColumnLoop:
  126.                 Emitw ROL_AL          ; emit code to adjust sprite
  127.                 Emitw SHR_CX          ; for new column
  128.                 Emitw ADC_SI_IMMED
  129.                 Emitb 0
  130.  
  131.                 inc dx
  132.                 cmp dx,[column]
  133.                 jl ColumnLoop
  134.  
  135.                 Emitb OUT_AL          ; emit code to set VGA mask for new column
  136.                 mov [set_column],dx
  137. SameColumn:
  138.             mov dx,[outputy]          ; calculate output position
  139.             add dx,[outputx]
  140.             sub dx,128
  141.  
  142.             add word ptr [scanx],4
  143.             mov cx,[scanx]            ; within four pixels of right edge?
  144.             cmp cx,[bwidth]
  145.             jge OnePixel
  146.  
  147.                 inc word ptr [outputx]
  148.         mov ah,[si+bx+4]           ; get second pixel
  149.         or   ah,ah
  150.                 jnz  TwoPixels
  151. OnePixel:
  152.                     cmp dx,127                ; can we use shorter form?
  153.                     jg OnePixLarge
  154.                     cmp dx,-128
  155.                     jl OnePixLarge
  156.                         Emitw SHORT_STORE_8
  157.                         Emitb dl              ; 8-bit position in output
  158.                         jmp EmitOnePixel
  159. OnePixLarge:
  160.                         Emitw STORE_8
  161.                         Emitw dx              ; position in output
  162. EmitOnePixel:
  163.                     Emitb al
  164.             jmp short Advance
  165. TwoPixels:
  166.                     push ax
  167.                     cmp dx,127
  168.                     jg TwoPixLarge
  169.                     cmp dx,-128
  170.                     jl TwoPixLarge
  171.                         Emitw SHORT_STORE_16
  172.                         Emitb dl              ; 8-bit position in output
  173.                         jmp EmitTwoPixels
  174. TwoPixLarge:
  175.                         Emitw STORE_16
  176.                         Emitw dx              ; position in output
  177. EmitTwoPixels:
  178.                     pop ax
  179.                     Emitw ax
  180.  
  181. Advance:
  182.         inc word ptr [outputx]
  183.         mov ax,[scanx]
  184.         add ax,4
  185.         cmp ax,[bwidth]
  186.         jl AdvanceDone
  187.             mov dx,[outputy]
  188.             add dx,[logical_width]
  189.             mov cx,[scany]
  190.             add cx,[bwidth]
  191.             cmp cx,[input_size]
  192.             jl NoNewColumn
  193.                 inc word ptr [column]
  194.                 mov cx,[column]
  195.                 cmp cx,4
  196.                 je Exit               ; Column 4: there is no column 4.
  197.                 xor cx,cx             ; scany and outputy are 0 again for
  198.                 mov dx,cx             ; the new column
  199. NoNewColumn:
  200.             mov [outputy],dx
  201.             mov [scany],cx
  202.             mov word ptr [outputx],0
  203.             mov ax,[column]
  204. AdvanceDone:
  205.             mov [scanx],ax
  206.     jmp MainLoop
  207.  
  208. Exit:
  209.     Emitb RETURN
  210.     mov ax,di
  211.     sub ax,word ptr [output]          ; size of generated code
  212.  
  213.     pop ds
  214.     pop di
  215.     pop si
  216.     mov sp,bp
  217.     pop bp
  218.  
  219.     ret
  220. _x_compile_bitmap endp
  221.  
  222.  
  223. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  224. ; _x_put_cbitmap
  225. ;
  226. ; Displays a compiled bitmap generated by x_compile_bitmap at given
  227. ; coordinates, on a given screen page.
  228. ;
  229. ; C near-callable as:
  230. ; void x_put_cbitmap (int XPos, int YPos,
  231. ;                     unsigned int PageOffset, char far * Sprite);
  232. ; ax, bx, cx, and dx are squashed like insignificant insects.
  233.  
  234.     align   2
  235. _x_put_cbitmap proc
  236. ARG XPos:word,YPos:word,PageOffset:word,Sprite:dword
  237.  
  238.     push bp
  239.     mov bp, sp
  240.     push si
  241.     push ds
  242.  
  243.     mov ax,[_ScrnLogicalByteWidth]    ; global Xlib variable
  244.     mul word ptr [YPos]               ; height in bytes
  245.     mov si,[XPos]
  246.     mov bx,si
  247.     shr bx,1
  248.     shr bx,1                          ; width in VGA planes
  249.     add bx,ax
  250.     add bx,[PageOffset]               ;     (YPos * screen width) +
  251.     add bx,128                        ; ==> (Xpos / 4) + page base - 128
  252.  
  253.     mov dx,SC_INDEX
  254.     mov al,MAP_MASK
  255.     out dx,al
  256.     inc dx                            ; ready to send out other masks as bytes
  257.  
  258.     and si,3                          ; XPos & 3 = column
  259.     mov al,ColumnMask[si]
  260.     out dx,al
  261.     mov cl,ColumnAdds[si]
  262.  
  263.     mov si,bx                       ; position of upper-left corner of sprite
  264.  
  265.     mov bx,SCREEN_SEG
  266.     mov ds,bx                   ; We do this so the compiled shape won't need
  267.                                 ;  segment overrides.
  268.  
  269.     call dword ptr [Sprite]     ; the business end of the routine
  270.  
  271.     pop ds
  272.     pop si
  273.     pop bp
  274.  
  275.     ret
  276. _x_put_cbitmap endp
  277.  
  278. end
  279.  
  280.